/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
ConfigurationWindow = function() {
	this.configuration;
    this.configurationName = new Ext.form.TextField({
        fieldLabel: 'Configuration Name',
        emptyText: '',
        width: 313,
        validationEvent: false,
        validateOnBlur: false,
        msgTarget: 'under',
        triggerAction: 'all',
        mode: 'local',
        listeners:{scope: this}
    });

    this.configurationDesc = new Ext.form.TextArea({
        fieldLabel: 'Configuration Description',
    	width: 313,
	    validationEvent: false,
        validateOnBlur: false,
        msgTarget: 'under',
        triggerAction: 'all',
        mode: 'local',
        listeners:{scope: this}
    });

    this.configurationRWan = new Ext.form.Checkbox({
        boxLabel: 'Wan configuration',
        width: 160,
        validationEvent: false,
        validateOnBlur: false,
        triggerAction: 'all',
        mode: 'local',
        listeners:{scope: this}
    });

    this.configurationRUsers = new Ext.form.Checkbox({
        boxLabel: 'Users configuration',
        width: 140,
        validationEvent: false,
        validateOnBlur: false,
        triggerAction: 'all',
        mode: 'local',
        listeners:{scope: this}
    });

    this.configurationCheckboxs = new Ext.Panel({
		layout:'column',
        fieldLabel: 'Restore',
        items: [this.configurationRWan,this.configurationRUsers],
        border: false,
        bodyStyle:'background:transparent;padding-left:10px;'
    });

    this.form = new Ext.FormPanel({
        labelAlign:'top',
        items: [this.configurationName,this.configurationDesc,this.configurationCheckboxs],
        border: false,
        bodyStyle:'background:transparent;padding:10px;'
    });

	ConfigurationWindow.superclass.constructor.call(this, {
        title: 'Create New Configuration',
        iconCls: 'configuration-icon',
        autoHeight: true,
        width: 350,
        resizable: false,
        plain:true,
        modal: true,
        y: 100,
        autoScroll: false,
        closeAction: 'hide',

        buttons:[{
            text: 'Create',
            handler: this.onCreate,
            scope: this
        },{
            text: 'Save',
            handler: this.onSave,
            scope: this
        },{
            text: 'Cancel',
            handler: this.hide.createDelegate(this, [])
        }],

        items: this.form
    });
}

Ext.extend(ConfigurationWindow, Ext.Window, {
    
    show : function(configuration){
        this.configuration = configuration;
        
        if(this.configuration) {
        	this.setTitle('Edit Configuration');
        	this.configurationName.setValue(this.configuration.name);
        	this.configurationRWan.setValue(this.configuration.rwan);
        	this.configurationRUsers.setValue(this.configuration.rusers);
        	this.configurationDesc.setValue(this.configuration.description);
        	this.buttons[0].hide();
        	this.buttons[1].show();
        } else {
        	this.setTitle('Create New Configuration');
        	this.configurationName.setValue('');
        	this.configurationRWan.setValue(false);
        	this.configurationRUsers.setValue(false);
        	this.configurationDesc.setValue('');
        	this.buttons[1].hide();
        	this.buttons[0].show();
        }

        ConfigurationWindow.superclass.show.apply(this, arguments);
    },

    onCreate: function() {
        if(this.configurationName.getValue() == '')
       	{
       		this.configurationName.markInvalid("Invalid configuration name!");
       	} else { 
	        this.el.mask('Creating New Configuration...', 'x-mask-loading');
	       	SGui.createConfiguration(this.configurationName.getValue(),this.configurationRWan.getValue(),this.configurationRUsers.getValue(),this.configurationDesc.getValue(),this.validateConfiguration,this);
       	}
    },

    onSave: function() {
		if(this.configurationName.getValue() == '')
       	{
       		this.configurationName.markInvalid("Invalid configuration name!");
       	} else { 
	        this.el.mask('Saving Configuration...', 'x-mask-loading');
	       	SGui.saveConfiguration(this.configuration.id,this.configurationName.getValue(),this.configurationRWan.getValue(),this.configurationRUsers.getValue(),this.configurationDesc.getValue(),this.validateConfiguration,this);
       	}
    },

    markInvalid : function(msg){
        this.configurationName.markInvalid(msg);
        this.el.unmask();
    },

    validateConfiguration : function(obj,result){
    	if(result.length > 1) {
    		if(result[0] == '1') {
                obj.el.unmask();
                obj.hide();
   				return obj.fireEvent('validconfiguration', result[1]);
    		} else {
    			obj.markInvalid(result[1]);
    		}    		
    	} else {
    		alert('Invalid Protocol From Server...');
    	}
	}
});
